package com.yc.store.biz;

import com.yc.store.bean.Result;
import com.yc.store.bean.User;
import com.yc.store.mapper.UserMapper;
import com.yc.store.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.regex.Pattern;

@Service
public class UserBiz {

    @Autowired
    UserMapper userMapper;

    @Transactional(rollbackFor = BizExeption.class)
    public Result test(User user, String repwd) throws BizExeption{

        Pattern phone = Pattern.compile("[1-9]{11}");


        Pattern email = Pattern.compile("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$/\n");

        Result result;
        if(userMapper.selectByName(user.getName()) > 0){
            result = new Result(0, "已有一个同名账号存在", null);
        }else if(!user.getPwd().equals(repwd)){
            result = new Result(0, "两此输入的密码不一致", null);
        }else{
            register(user);
            result = new Result(1, "注册成功", user);
        }
        return result;
    }

    public void register(User user) {
        userMapper.insert(user);
    }
}
